package com.xmhmyh.generalframework.project.sys.controller;

import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.conversion.Bean2SelectConverter;
import com.xmhmyh.generalframework.common.conversion.Enum2SelectVOConversion;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.LogBusType;
import com.xmhmyh.generalframework.common.enums.LogModule;
import com.xmhmyh.generalframework.common.enums.LogSourceType;
import com.xmhmyh.generalframework.common.enums.ResponseCode;
import com.xmhmyh.generalframework.common.vo.SelectVO;
import com.xmhmyh.generalframework.framework.aspectj.annotation.Log;
import com.xmhmyh.generalframework.framework.redisQueue.RedisQueueMessage;
import com.xmhmyh.generalframework.framework.redisQueue.RedisQueueProducer;
import com.xmhmyh.generalframework.framework.redisQueue.consumers.MyConsumer;
import com.xmhmyh.generalframework.framework.security.LoginUser;
import com.xmhmyh.generalframework.framework.security.TokenService;
import com.xmhmyh.generalframework.project.sys.dto.LoginDTO;
import com.xmhmyh.generalframework.project.sys.dto.MenuDTO;
import com.xmhmyh.generalframework.project.sys.entity.Menu;
import com.xmhmyh.generalframework.project.sys.entity.Role;
import com.xmhmyh.generalframework.project.sys.entity.TreeNode;
import com.xmhmyh.generalframework.project.sys.entity.User;
import com.xmhmyh.generalframework.project.sys.enums.MenuType;
import com.xmhmyh.generalframework.project.sys.service.IMenuService;
import com.xmhmyh.generalframework.project.sys.service.IRoleService;
import com.xmhmyh.generalframework.project.sys.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: 于丁
 * @DateTime: 2020/7/21 4:02 下午
 */
@Slf4j
@RestController
@Api(tags = "菜单相关接口")
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {


    @Autowired
    private IMenuService menuService;


    /**
     * @Author: 于丁
     * @Description: 获取菜单列表信息
     * @DateTime: 2020/7/28 2:41 下午
     * @Params:
     * @Return
     */
    @ApiOperation(value = "获取菜单列表信息")
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("getMenuListData")
    public Response getMenuListDataHandler() {
        LoginUser loginUser = getLoginUser();
        List<MenuVO> menuVOList = menuService.getMenuListDataUser(loginUser.getUser().getId());
        return Response.success(menuVOList);
    }

    /**
     * @Author: 于丁
     * @Description: 获取菜单类型数据
     * @DateTime: 2020/7/29 9:33 上午
     * @Params:
     * @Return
     */
    @ApiOperation(value = "获取菜单类型数据")
    @PreAuthorize("hasAnyAuthority('system:menu:edit','system:menu:add')")
    @PostMapping("getMenuTypeData")
    public Response getMenuTypeDataHandler() {
        return Response.success(Enum2SelectVOConversion.conversion(MenuType.class));
    }

    /**
     * @Author: 于丁
     * @Description: 获取不带按钮的菜单树
     * @DateTime: 2021/4/28 11:53 上午
     * @Params: * @param: null
     * @Return {@link null}
     */

    @ApiOperation(value = "获取菜单类型数据")
    @PostMapping("getMenuTreeNoBtn")
    public Response getMenuTreeNoBtnHandler() {
        User user = getLoginUser().getUser();
        Collection<Menu> menus = menuService.getMenuByUserId(user.getId());
        List<TreeNode> treeNodes = new ArrayList<>();
        // 外层添加主目录
        TreeNode treeNode = new TreeNode();
        treeNode.setId(0L);
        treeNode.setChildren(menuService.buildMenuTree(menus, false));
        treeNode.setLabel("主目录");
        treeNode.setLeaf(false);
        treeNodes.add(treeNode);
        return Response.success(treeNodes);
    }

    /**
     * @Author: 于丁
     * @Description: 保存菜单数据
     * @DateTime: 2020/7/29 11:35 上午
     * @Params:
     * @Return
     */
    @Log(busType = LogBusType.EDITANDSAVE, source = LogSourceType.MANAGE, module = LogModule.MENUMANAGE)
    @ApiOperation(value = "保存菜单")
    @PreAuthorize("hasAuthority('system:menu:save')")
    @PostMapping("saveMenu")
    public Response saveMenuHandler(@RequestBody MenuDTO menuDTO) {
        LoginUser loginUser = getLoginUser();
        menuService.saveMenuHandler(loginUser.getUser(), menuDTO);
        return Response.success();
    }


    /**
     * @Author: 于丁
     * @Description: 删除菜单
     * @DateTime: 2020/7/29 3:57 下午
     * @Params:
     * @Return
     */
    @Log(busType = LogBusType.DELETE, source = LogSourceType.MANAGE, module = LogModule.MENUMANAGE)
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasAuthority('system:user:delete')")
    @PostMapping("del")
    public Response delHandler(@RequestParam(required = true) Long id) {
        menuService.delHandler(id);
        return Response.success();
    }


    @ApiOperation(value = "获取模块按钮")
    @PostMapping("getMenuBtn")
    public Response getMenuBtnHandler(Long menuId) {
        List<MenuBtnVO> menuBtnVOList = menuService.getMenuBtnHandler(getLoginUser().getMenuIdsMap(), menuId);
        return Response.success(menuBtnVOList);
    }


}
